package jmine.tec.di.type;

import jmine.tec.di.DependencyInjectionMessages;
import bancosys.tec.exception.AbstractException;
import bancosys.tec.exception.LocalizedMessageHolder;

/**
 * Excecao lancada se houver uma falha ao converter tipos
 * 
 * @author MarceloT
 */
public class TypeConvertionException extends AbstractException {
    private static final long serialVersionUID = 1614598160069105896L;

    private Class<?> targetType;

    private Object value;

    /**
     * C'tor
     * 
     * @param targetType o tipo alvo
     * @param value o valor a ser convertido
     */
    public TypeConvertionException(Class<?> targetType, Object value) {
        super(createMessageHolder(targetType, value));
        this.targetType = targetType;
        this.value = value;
    }

    /**
     * C'tor
     * 
     * @param cause cause
     * @param targetType o tipo alvo
     * @param value o valor a ser convertido
     */
    public TypeConvertionException(Exception cause, Class<?> targetType, Object value) {
        super(createMessageHolder(targetType, value), cause);
        this.targetType = targetType;
        this.value = value;
    }

    /**
     * C'tor.
     * 
     * @param targetType o tipo que o valor será convertido.
     * @param value o valor a ser convertido
     * @param message o message holder
     */
    public TypeConvertionException(Class<?> targetType, Object value, LocalizedMessageHolder message) {
        super(message);
        this.targetType = targetType;
        this.value = value;
    }

    /**
     * Constructor
     * 
     * @param messageHolder messageHolder
     * @param cause cause
     */
    public TypeConvertionException(LocalizedMessageHolder messageHolder, Throwable cause) {
        super(messageHolder, cause);
    }

    /**
     * Constructor
     * 
     * @param messageHolder messageHolder
     */
    public TypeConvertionException(LocalizedMessageHolder messageHolder) {
        super(messageHolder);
    }

    /**
     * @return the targetType
     */
    public Class<?> getTargetType() {
        return this.targetType;
    }

    /**
     * @return the value
     */
    public Object getValue() {
        return this.value;
    }

    /**
     * Cria um novo MessageHolder
     * 
     * @param targetType o tipo
     * @param value o valor
     * @return o MessageHolder
     */
    private static LocalizedMessageHolder createMessageHolder(Class<?> targetType, Object value) {
        return DependencyInjectionMessages.TYPE_CONVERTION_ERROR.create(targetType, value);
    }

}
